import { _decorator, Button, find, Label, Sprite, SpriteAtlas, SpriteFrame, tween, Tween, UITransform, Widget } from 'cc';
import { $planet_BaseUI_poof$ } from '../../../frame/component/ui.base';
import { $planet_PLANET_SPRITE_poof$ } from '../../config.global';
import $planet_GameGameAssets_poof$ from '../manager/game.game-assets';
import $planet_GameDC_poof$ from '../data/game.data.center';
import { $planet_GameFloat_poof$ } from './game.float';
const { ccclass, property } = _decorator;

@ccclass('$planet_GameHomeUI_poof$')
export class $planet_GameHomeUI_poof$ extends $planet_BaseUI_poof$ {
	@property(SpriteAtlas)
	private planet_atlas: SpriteAtlas = null;

	private ui_top: UITransform;
	private ui_bottom: UITransform;

	private va_top: abd.$planet_cc_poof$.$planet_VecAssist_poof$;
	private va_bottom: abd.$planet_cc_poof$.$planet_VecAssist_poof$;
	private $planet_sprite_planet_map_poof$: Map<string, SpriteFrame>;

	protected $planet_initAfterOnLoad_poof$(): void {
		[this.ui_top, this.ui_bottom] = ['top', 'bottom'].map(name=>this.node.getChildByName(name).getComponent(UITransform));
		[this.va_top, this.va_bottom] = [this.ui_top, this.ui_bottom].map(ui=>new abd.$planet_cc_poof$.$planet_VecAssist_poof$(ui.node));
		this.$planet_sprite_planet_map_poof$ = new Map(this.planet_atlas.getSpriteFrames().map(sframe=>[sframe.name, sframe]));
	}

	protected start(): void {
		this.$planet_init_poof$();
	}

	private $planet_init_poof$(){
		this.ui_top.node.setPosition(this.va_top.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_top_poof$'));
		this.ui_bottom.node.setPosition(this.va_bottom.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_bottom_poof$'));

		this.node.active = false;

		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();
	}

	protected $planet_listening_poof$(){
		let lbl_elsbcount = this.ui_top.node.getChildByName('lbl-elsb-count').getComponent(Label);
		let icon_elsb_statistics = find('elsb-statistics/icon', this.ui_top.node).getComponent(Sprite);

		let list = [];

		Promise.all([
			$planet_GameDC_poof$.$planet_follow_poof$('$planet_trigger_poof$', '$planet_UI_HOME_poof$', (o, show)=>{
				if(show===o){
					return void 0;
				}else if(show){
					this.$planet_enter_poof$();
				}else{
					this.$planet_leave_poof$();
				}
			}, false),
			$planet_GameDC_poof$.$planet_follow_poof$('$planet_user_poof$', '$planet_count_elsb_poof$', (_, v)=>{
				lbl_elsbcount.string = $planet_GameGameAssets_poof$.$planet_getLangText_poof$(2, v??$planet_GameDC_poof$.$planet_user_poof$.$planet_count_elsb_poof$);
			}),
			$planet_GameDC_poof$.$planet_follow_poof$('$planet_trigger_poof$', '$planet_NEW_ELSB_poof$', (_, id)=>{
				$planet_GameFloat_poof$.$planet_moveElsb_poof$(icon_elsb_statistics.node)
				.then(()=>{
					$planet_GameFloat_poof$.$planet_destroyElsb_poof$();
					$planet_GameDC_poof$.$planet_user_poof$.$planet_count_elsb_poof$++;
					$planet_GameDC_poof$.$planet_user_poof$.$planet_elsb_poof$++;
				});
			}, false),
			$planet_GameDC_poof$.$planet_follow_poof$('$planet_trigger_poof$', '$planet_RESET_poof$', (o, v)=>{
				if(v==o || v) return void 0;
				this.$planet___btn_map___poof$.get('reset').interactable = true
			}, false),
			$planet_GameDC_poof$.$planet_follow_poof$('$planet_user_poof$', '$planet_target_planet_poof$', (_, id)=>{
				let sframe = this.$planet_sprite_planet_map_poof$.get($planet_PLANET_SPRITE_poof$[id]);
				if(sframe){
					abd.$planet_cc_poof$.$planet_skinPeeler_poof$(icon_elsb_statistics.node, sframe, {height: true});
				}
			}),
		]).then(ls=>list.push(...ls));

		return list;
	}

	protected $planet__click_event__poof$: { [name: string]: (this: $planet_GameHomeUI_poof$, button: Button) => void; } = {
		reset(btn){
			btn.interactable = false;
			$planet_GameDC_poof$.$planet_trigger_poof$.$planet_RESET_poof$ = true;
		}
	}

	private $planet_enterBefore_poof$(){
	}

	private $planet_enterAfter_poof$(){
	}

	private $planet_enter_poof$(){
		this.$planet_enterBefore_poof$();
		let duration = 0.5;
		let np = new abd.$planet_NudityPromise_poof$<0>();
		this.node.active = true;
		this.scheduleOnce(()=>np.$planet_resolve_poof$(0), duration+0.05);

		this.va_top.$planet_getPosition_poof$('$planet_center_poof$', '$planet_top_poof$');
		this.va_bottom.$planet_getPosition_poof$('$planet_center_poof$', '$planet_bottom_poof$');
		if(abd.$planet_cc_poof$.$planet_is_long_screen_poof$) this.va_top.$planet_vec_poof$.add3f(0, -100, 0);

		(<const>[
			[this.ui_top, this.va_top],
			[this.ui_bottom, this.va_bottom],
		]).forEach(([ui, va])=>{
			Tween.stopAllByTarget(ui.node);
			tween(ui.node).to(duration, {position: va.$planet_vec_poof$}, {easing: 'quartInOut'}).start();
		});

		np.$planet_promise_poof$.then(()=>this.$planet_enterAfter_poof$());

		return np.$planet_promise_poof$;
	}

	private $planet_leaveBefore_poof$(){
		$planet_GameFloat_poof$.$planet_hidePointer_poof$();
	}

	private $planet_leave_poof$(){
		this.$planet_leaveBefore_poof$();
		let duration = 0.5;
		let np = new abd.$planet_NudityPromise_poof$<0>();
		this.scheduleOnce(()=>np.$planet_resolve_poof$(0), duration+0.05);

		this.va_top.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_top_poof$');
		this.va_bottom.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_bottom_poof$');

		(<const>[
			[this.ui_top, this.va_top],
			[this.ui_bottom, this.va_bottom],
		]).forEach(([ui, va])=>{
			Tween.stopAllByTarget(ui.node);
			tween(ui.node).to(duration, {position: va.$planet_vec_poof$}, {easing: 'quartInOut'}).start();
		});

		np.$planet_promise_poof$.then(()=>this.node.active = false);

		return np.$planet_promise_poof$;
	}
}

